Technical Q&As


The Apple Media Tool and Apple Media Tool Programming Environment products have been discontinued. For more information check out: AMT/PE Discontinued.

AMT_PE 05 - Defining a Data Structure in AML and Accessing it in C (1-Aug-95)


Q I have a two-part question: First, I want to create a 2 column spreadsheet with N rows. If I were to do it in C, it would be an array of data structures:
    typedef struct {
            int   iKEY;
            char  szString[30];
    } MYCELLS;
    struct MYCELLS spreadsheet[10];

How do I express this structure in Apple Media Language?

Second, one column of the array is to be populated from a C extension program. How do you set the elements of this AML object from C?

A To express this structure in Apple Media Language, use the following code snippet:

-- first, here is how you define the MYCELLS struct in AML
class MYCELLS is ANY;
has
	iKey; -- INTEGER; though not explicitly typed as such
	szString; -- STRING ; though not explicitly typed as such
end;

-- now here's an AML array of ten MYCELLS
object MY_TWO_D_ARRAY is COLLECTION 
has
	-- add your own methods here. This one's just an example
	GetAndSetItem5()
		external "keyGetAndSetItem5";

with items is
[
-- I allocate the 10 cells statically, you could do it 
-- dynamically at runtime.
-- note iKey has no correspondence to szString
	MYCELLS with iKey is 1; szString is "foo"; end,
	MYCELLS with iKey is 2; szString is "a string"; end,
	MYCELLS with iKey is 33; szString is "random"; end,
	MYCELLS with iKey is 4; szString is "stuff"; end,
	MYCELLS with iKey is 12; szString is "etc"; end,
	MYCELLS with iKey is 18; szString is "and"; end,
	MYCELLS with iKey is 7; szString is "more stuff"; end,
	MYCELLS with iKey is 234; szString is "whatever you like"; end,
	MYCELLS with iKey is 9; szString is "can go"; end,
	MYCELLS with iKey is 10; szString is "here"; end
];
end;

To access the members of the structure in C, you can say:

#include "key.h";
extern const keyID K_IKEY; // the "ikey" field of the MYCELLS class
extern const keyID K_SZSTRING; // the "szString" field of the MYCELLS class

void keyGetAndSetItem5(key * the)
{
	int aKey;
	int anIndex;
	char aString[30];
	key* a = keyUse(1);  // a temporary key object, makes the code 
						 // a little more readable

	// this fetches the 5th item in the array and assign the
	// iKey value to aKey and the szString value to aString...
	
	anIndex = 5;  // I want the 5th item
	
	// get an item of the array
	a[0] = keyCall1(the[SELF], K_GETAT, keyFromInteger(anIndex));

	aKey = keyToInteger (keyGet(a[0], K_IKEY));
	strcpy(aString, keyToString(keyGet(a[0], K_SZSTRING)));
	
	// aKey now contains 12 and aString contains "etc".
	
	// To set the values of aKey and aString:

	 keyPut (a[0], K_IKEY, keyFromInteger(17));
	 keyPut (a[0], K_SZSTRING, keyFromString ("new contents"));
}



Technical Q&As
Previous Question | Contents | Next Question